package cn.swust.designpatternlearningplatform.controller;

import cn.swust.designpatternlearningplatform.common.AnnotationUtils.Authentication;
import cn.swust.designpatternlearningplatform.common.Result.R;
import cn.swust.designpatternlearningplatform.common.utils.AuthUserContext;
import cn.swust.designpatternlearningplatform.domain.dto.StudentPaperListDto;
import cn.swust.designpatternlearningplatform.domain.dto.TestPaperDto;
import cn.swust.designpatternlearningplatform.domain.enums.RoleEnum;
import cn.swust.designpatternlearningplatform.domain.vo.StudentSubmitTestDataVo;
import cn.swust.designpatternlearningplatform.service.StudentTestService;
import cn.swust.designpatternlearningplatform.service.TeacherTestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@RestController
@Slf4j
@RequestMapping("/studentTest")
@Authentication(RoleEnum.STUDENT)
public class StudentTestController {
    @Resource
    private TeacherTestService teacherTestService;
    @Resource
    private StudentTestService studentTestService;
    /**
     * @param studentSubmitTestDataVo:
      * @return R<String>
     * @author wang
     * description 提交做题的考试答案
     * date 2023/5/15 11:37
     */
    @RequestMapping("/submitTestData")
    public R<String> submitTestData(@RequestBody StudentSubmitTestDataVo studentSubmitTestDataVo){
        String studentNumber= AuthUserContext.get().getNumber();
        int flag=0;
        try {
            studentTestService.postAnswer(studentSubmitTestDataVo,studentNumber);
            flag=0;
        }
        catch (Exception e)
        {
            log.info(String.valueOf(e));
            flag=1;
        }
        finally {
            if ((flag==0))
            {
                return R.success("提交成功");
            }
            else {
                return R.error("提交失败");
            }
        }

    }

    /**
     * @param :
      * @return R<Object>
     * @author wang
     * description 获取学生的考试列表
     * date 2023/5/15 11:38
     */

    @RequestMapping("/getTestList")
    public R<List<StudentPaperListDto>> getTestList(){
        String studentNumber=AuthUserContext.get().getNumber();
        List<StudentPaperListDto> list=studentTestService.getTestList(studentNumber);
        return R.success(list);
    }


}
